package com.nbot.serial;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.alibaba.fastjson.JSON;
import com.nbot.common.InterpreterFactory;
import com.nbot.common.command.BaseCommand;
import gnu.io.*;

public class ContinueRead extends Thread implements SerialPortEventListener { // SerialPortEventListener
    // 监听器,独立开辟一个线程监听串口数据
    static CommPortIdentifier portId; // 串口通信管理类
    static Enumeration<?> portList; // 有效连接上的端口的枚举
    InputStream inputStream; // 从串口来的输入流
    static OutputStream outputStream;// 向串口输出的流
    static SerialPort serialPort; // 串口的引用
    // 堵塞队列用来存放读到的数据
    private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();
    byte[] realDataBuf = null;//真实数据buf
    int infoNowIndex = 0;//当前读写位置
    int[] judgeSwitch = new int[3];//判断标识

    @Override
    /**
     * SerialPort EventListene 的方法,持续监听端口上是否有数据流
     */
    public void serialEvent(SerialPortEvent event) {//

        switch (event.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据
                byte[] readBuffer = new byte[64];
                try {
                    int numBytes = -1;
                    while (inputStream.available() > 0) {
                        numBytes = inputStream.read(readBuffer);
                        if (numBytes > 0) {
                            for (int i=0;i<numBytes;i++){
                                if (readBuffer[i] == (byte) 0xAC){
                                    judgeSwitch[0] = 1;
                                }else if (judgeSwitch[0] == 1){
                                    if (readBuffer[i] == (byte) 0xDF){
                                        judgeSwitch[1] = 1;
                                        realDataBuf = new byte[1024];
                                    }else{
                                        realDataBuf[infoNowIndex] = (byte) 0xAC;
                                        infoNowIndex += 1;
                                        realDataBuf[infoNowIndex] = readBuffer[i];
                                        infoNowIndex++;
                                    }
                                    judgeSwitch[0] = 0;
                                }else if (readBuffer[i] == (byte) 0xFF){
                                    judgeSwitch[2] = 1;
                                }else if (judgeSwitch[2] == 1){
                                    if (readBuffer[i] == (byte) 0xEE) {
                                        msgQueue.add(new String(realDataBuf,"unicode"));
                                        realDataBuf = null;
                                        infoNowIndex = 0;
                                        judgeSwitch = new int[3];
                                    } else {
                                        judgeSwitch[2] = 0;
                                        realDataBuf[infoNowIndex] = (byte) 0xFF;
                                        infoNowIndex += 1;
                                        realDataBuf[infoNowIndex] = readBuffer[i];
                                        infoNowIndex++;
                                    }
                                }else if (judgeSwitch[1] == 1 && judgeSwitch[2] == 0){
                                    realDataBuf[infoNowIndex] = readBuffer[i];
                                    infoNowIndex++;
                                }
                            }
//                            System.err.println(new String(readBuffer,"unicode"));
//                            msgQueue.add(new Date() + "真实收到的数据为：---" + new String(readBuffer,"utf-8"));
                            readBuffer = new byte[64];// 重新构造缓冲对象，否则有可能会影响接下来接收的数据
                        } else {
                            msgQueue.add("额---没有读到数据");
                        }
                    }
                } catch (IOException e) {
            }
            break;
        }
    }

    /**
     *
     * 通过程序打开COMn串口，设置监听器以及相关的参数
     *
     * @return 返回1 表示端口打开成功，返回 0表示端口打开失败
     */
    public int startComPort() {
        // 通过串口通信管理类获得当前连接上的串口列表
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {

            // 获取相应串口对象
            portId = (CommPortIdentifier) portList.nextElement();

            System.out.println("设备类型：--->" + portId.getPortType());
            System.out.println("设备名称：---->" + portId.getName());
            // 判断端口类型是否为串口
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                // 判断如果串口存在，就打开该串口
                if (portId.getName().equals("COM4")) {
                    try {
                        // 设置打开串口名字,延迟为2毫秒
                        serialPort = (SerialPort) portId.open("COM_4", 2000);

                    } catch (PortInUseException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    // 设置当前串口的输入输出流
                    try {
                        inputStream = serialPort.getInputStream();
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    // 给当前串口添加一个监听器
                    try {
                        serialPort.addEventListener(this);
                    } catch (TooManyListenersException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    // 设置监听器生效，即：当有数据时通知
                    serialPort.notifyOnDataAvailable(true);

                    // 设置串口的一些读写参数
                    try {
                        // 比特率、数据位、停止位、奇偶校验位
                        serialPort.setSerialPortParams(115200,
                                SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {
                        e.printStackTrace();
                        return 0;
                    }

                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public void run() {
        try {
            System.out.println("--------------任务接收处理线程运行了--------------");
            while (true) {
                // 如果堵塞队列中存在数据就将其输出
                if (msgQueue.size() > 0) {
                    String[] rs = msgQueue.take().split("@");
                    BaseCommand reciceCommand = (BaseCommand)JSON.parseObject(rs[0],Class.forName(rs[1].trim()));
                    InterpreterFactory.getInterpreter(reciceCommand).execute();
                    System.err.println(rs[0]);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(byte[] data){
        try {
//            System.out.println("发出字节数：" + abc.getBytes("utf-8").length);
           // outputStream.write(data.getBytes("utf-8"), 0,data.getBytes("utf-8").length);
//            byte[] a = {(byte) 0xFD, (byte) 0xCA};
            outputStream.write(data,0,data.length);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    public static void main(String[] args) {
//        ContinueRead cRead = new ContinueRead();
//        int i = cRead.startComPort();
//        if (i == 1) {
//            // 启动线程来处理收到的数据
//            cRead.start();
//            try {
//                String st = "哈哈----你好";
//                System.out.println("发出字节数：" + st.getBytes("gbk").length);
//                outputStream.write(st.getBytes("gbk"), 0,
//                        st.getBytes("gbk").length);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        } else {
//            return;
//        }
//    }
}